package com.nuscriptions.exchange.frameworkcore.filter;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Properties;


/**
 *  Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)   拦截执行器的方法
	ParameterHandler (getParameterObject, setParameters)	拦截参数的处理
	ResultSetHandler (handleResultSets, handleOutputParameters)	拦截结果集的处理
	StatementHandler (prepare, parameterize, batch, update, query) 拦截Sql语法构建的处理
 * @author Administrator
 *
 */
@Intercepts({@Signature(type = Executor.class,method = "update",args = {MappedStatement.class,Object.class})})
@Component
public class HandleTimeInterceptor implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		 Object[] args = invocation.getArgs();
		 MappedStatement mappedStatement = (MappedStatement) args[0];
		 SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
//		 if(sqlCommandType== SqlCommandType.UPDATE) {
//			 if(args[1] instanceof BaseEntity) {
//				BaseEntity baseEntity = (BaseEntity) args[1];
//				baseEntity.setUpdateTime(new Date());
//				String userId="";
//				 try
//				 {
//					 userId= SecurityUtils.getUserId().toString();
//				 }catch (Exception e){
//					// throw new BaseException("当前没有登录人");
//
//				 }
//				baseEntity.setUpdateBy(userId);
//			 }
//		 }else if(sqlCommandType==SqlCommandType.INSERT) {
//			 if(args[1] instanceof BaseEntity) {
//				BaseEntity baseEntity = (BaseEntity) args[1];
//				baseEntity.setCreateTime(new Date());
//				String userId="";
//				try
//				{
//				 userId= SecurityUtils.getUserId().toString();
//				}catch (Exception e){
//					//throw new BaseException("当前没有登录人");
//				}
//				baseEntity.setCreateBy(userId);
//			}
//		 }

		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
	}
}
